package org.example.tx.user;

import jakarta.annotation.Resource;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class UserInitializer implements InitializingBean {

    @Resource(name = "jdbcTemplate1")
    private JdbcTemplate jdbcTemplate;

    @Override
    public void afterPropertiesSet() throws Exception {
        jdbcTemplate.execute("create table if not exists `user` (" +
                "    `id` int not null auto_increment primary key," +
                "    `name` varchar(255) not null" +
                ")");

        String insertAclSql = "replace into `user` (`id`, `name`) values (?, ?)";
        jdbcTemplate.batchUpdate(insertAclSql, List.of(
                new Object[]{1001, "user1"},
                new Object[]{1002, "user2"},
                new Object[]{1003, "user3"}
        ));
    }
}
